package com.qf.web.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.aop.aspect.ApiOperationLog;
import com.qf.aop.aspect.ApiOperationLogTitle;
import com.qf.api.blog.dto.NameDTO;
import com.qf.api.blog.entity.TagEntity;
import com.qf.api.blog.service.TagService;
import com.qf.api.blog.vo.NameVO;
import com.qf.api.blog.vo.OptionsVO;
import com.qf.api.sys.mybatis.page.BaseController;
import com.qf.api.sys.mybatis.page.PageResult;
import com.qf.core.enums.common.BusinessTypeEnum;
import com.qf.core.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: sin
 * @Date: 2025/5/23 18:46
 * @Description:
 **/
@RestController
@ApiOperationLogTitle("标签模块")
@RequestMapping("/admin/tag")
@Tag(name = "02.标签管理")
public class AdminTagController extends BaseController {

    @Resource
    private TagService tagService;

    @GetMapping
    @Operation(summary = "获取标签列表")
    public ResponseResult<PageResult<NameVO>> getTagList(@RequestParam(value = "name", required = false) String tagName) {
        startPage();
        List<TagEntity> list = tagService.list(new QueryWrapper<TagEntity>()
                .like("name", tagName)
                .eq("deleted", 0)
                .orderByAsc("create_time")
        );
        List<NameVO> nameVOS = BeanUtil.copyToList(list, NameVO.class);
        return ResponseResult.success(getData(nameVOS));
    }

    @Operation(summary = "02.添加标签", description = "添加标签")
    @ApiOperationLog(desc = "添加标签", businessType = BusinessTypeEnum.ADD)
    @PostMapping
    public ResponseResult<Boolean> save(@RequestBody @Validated NameDTO nameDTO) {
        return isOk(tagService.addTag(nameDTO.getName()));
    }

    @PutMapping("/{id}")
    @Operation(summary = "03.更新标签")
    @ApiOperationLog(desc = "更新标签",businessType = BusinessTypeEnum.MODIFY)
    public ResponseResult<Boolean> updateTag(@PathVariable("id") @NotNull Long id, @RequestBody @Validated NameDTO nameDTO) {
        return isOk(tagService.updateTag(id, nameDTO.getName()));
    }

    @DeleteMapping
    @Operation(summary = "04.删除标签")
    @ApiOperationLog(desc = "删除标签", businessType = BusinessTypeEnum.DELETE)
    public ResponseResult<Boolean> deleteCategory(@RequestBody List<Long> ids) {
        // TODO 健壮性删除  后续优化
        return isOk(tagService.removeBatchByIds(ids));
    }

    @GetMapping("/options")
    @Operation(summary = "05.获取标签下拉列表")
    public ResponseResult<List<OptionsVO>> getCategoryOptionsVO() {
        List<TagEntity> list = tagService.list();
        return ResponseResult.success(BeanUtil.copyToList(list, OptionsVO.class));
    }
}
